package NewGW;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.regex.Pattern;

import utils.variable;


/***********************************************
 * Class used to write one line in the gateway
 ***********************************************/

public class oneLine
	{
	/************
	 * Variables
	 ***********/
	
	
	/**************************************
	 * Method used to send the command to 
	 * the gateway
	 **************************************/
	public static void writeInstruction(String command, int waitingTime) throws Exception
		{
		variable.getOut().write(command+variable.getSautDeLigne());
		variable.getOut().flush();
		variable.getTGW().sleep(waitingTime);
		}
	
	public static void writeInstruction(String command) throws Exception
		{
		variable.getOut().write(command+variable.getSautDeLigne());
		variable.getOut().flush();
		variable.getTGW().sleep(50);
		}
	
	public static void writeInstructionEnhanced(int index, int indexTemplate, int o, int i) throws Exception
		{
		String command = variable.defVar("",i,index,variable.getMaSheet(),variable.getGW().get(indexTemplate).get(o).getArr());
		int waitingTime = variable.getWaitingTime(indexTemplate, o, i);
		variable.getOut().write(command+variable.getSautDeLigne());
		variable.getOut().flush();
		variable.getTGW().sleep(waitingTime);
		}
	
	/**************************************
	 * Method used to send the command to 
	 * the gateway
	 **************************************/
	public static void writeSerialInstruction(String command, int waitingTime) throws Exception
		{
		variable.getSerialOut().write(convertStringToASCII(command+variable.getSautDeLigne()));
		variable.getTGW().sleep(waitingTime);
		}
	
	public static void writeSerialInstruction(String command) throws Exception
		{
		variable.getSerialOut().write(convertStringToASCII(command+variable.getSautDeLigne()));
		variable.getTGW().sleep(50);
		}
	
	
	public static void writeSerialInstructionEnhanced(int index, int indexTemplate, int o, int i) throws Exception
		{
		String command = variable.defVar("",i,index,variable.getMaSheet(),variable.getGW().get(indexTemplate).get(o).getArr());
		int waitingTime = variable.getWaitingTime(indexTemplate, o, i);
		variable.getSerialOut().write(convertStringToASCII(command+variable.getSautDeLigne()));
		variable.getTGW().sleep(waitingTime);
		}
	
	
	
	public static byte[] convertStringToASCII(String str)
		{
		try
			{
			byte[] b = str.getBytes("US-ASCII");
			return b;
			}
		catch(Exception exc)
			{
			exc.printStackTrace();
			System.out.println("Erreur : "+exc.getMessage());
			return null;
			}
		}
	
	/*2012*//*RATEL Alexandre 8)*/
	}
